/**
 * 菜单配置 - 基于角色的动态菜单生成
 */
import { 
  Monitor, 
  Ticket, 
  User, 
  Setting, 
  DataAnalysis, 
  Bell, 
  Document, 
  Files,
  Management,
  UserFilled,
  Tools
} from '@element-plus/icons-vue'

// 菜单项配置
export const MENU_ITEMS = [
  {
    key: 'dashboard',
    title: '仪表板',
    icon: Monitor,
    path: '/dashboard',
    roles: ['admin', 'agent', 'customer', 'supervisor'],
    permissions: []
  },
  {
    key: 'tickets',
    title: '工单管理',
    icon: Ticket,
    path: '/tickets',
    roles: ['admin', 'agent', 'customer', 'supervisor'],
    permissions: [],
    children: [
      {
        key: 'ticket-submit',
        title: '提交工单',
        icon: Document,
        path: '/submit',
        roles: ['admin', 'agent', 'customer', 'supervisor'],
        permissions: []
      },
      {
        key: 'ticket-list',
        title: '工单列表',
        icon: Ticket,
        path: '/tickets',
        roles: ['admin', 'agent', 'customer', 'supervisor'],
        permissions: []
      },
      {
        key: 'ticket-management',
        title: '工单管理',
        icon: Management,
        path: '/ticket-management',
        roles: ['admin', 'agent', 'supervisor'],
        permissions: ['ticket:assign']
      }
    ]
  },
  {
    key: 'statistics',
    title: '统计报表',
    icon: DataAnalysis,
    path: '/statistics-dashboard',
    roles: ['admin', 'agent', 'supervisor'],
    permissions: ['report:view']
  },
  {
    key: 'notifications',
    title: '通知中心',
    icon: Bell,
    path: '/notifications',
    roles: ['admin', 'agent', 'supervisor'],
    permissions: ['notification:manage']
  },
  {
    key: 'file-management',
    title: '文件管理',
    icon: Files,
    path: '/file-management',
    roles: ['admin', 'agent', 'supervisor'],
    permissions: ['file:manage']
  },
  {
    key: 'user-management',
    title: '用户管理',
    icon: UserFilled,
    path: '/user-management',
    roles: ['admin', 'supervisor'],
    permissions: ['user:manage']
  },
  {
    key: 'role-management',
    title: '角色管理',
    icon: Tools,
    path: '/role-management',
    roles: ['admin'],
    permissions: ['user:manage']
  },
  {
    key: 'system-settings',
    title: '系统设置',
    icon: Setting,
    path: '/system-settings',
    roles: ['admin'],
    permissions: ['system:config']
  },
  {
    key: 'personal-center',
    title: '个人中心',
    icon: User,
    path: '/personal-center',
    roles: ['admin', 'agent', 'customer', 'supervisor'],
    permissions: []
  },
  {
    key: 'permission-demo',
    title: '权限演示',
    icon: Tools,
    path: '/permission-demo',
    roles: ['admin', 'agent', 'customer', 'supervisor'],
    permissions: []
  }
]

// 根据用户角色和权限过滤菜单
export function getFilteredMenuItems(userRole, userPermissions = []) {
  // 确保角色是小写格式（后端可能返回大写，如 SUPERVISOR）
  const normalizedRole = userRole ? userRole.toLowerCase() : null
  if (!normalizedRole) return []
  
  return MENU_ITEMS.filter(item => {
    // 检查角色权限（支持大小写不敏感）
    const hasRoleAccess = item.roles.some(role => role.toLowerCase() === normalizedRole)
    if (!hasRoleAccess) return false
    
    // 检查权限
    if (item.permissions.length > 0) {
      const hasPermission = item.permissions.some(permission => 
        userPermissions.includes(permission)
      )
      if (!hasPermission) return false
    }
    
    // 如果有子菜单，递归过滤
    if (item.children) {
      item.children = item.children.filter(child => {
        const hasChildRoleAccess = child.roles.some(role => role.toLowerCase() === normalizedRole)
        if (!hasChildRoleAccess) return false
        
        if (child.permissions.length > 0) {
          return child.permissions.some(permission => 
            userPermissions.includes(permission)
          )
        }
        return true
      })
      
      // 如果所有子菜单都被过滤掉了，隐藏父菜单
      return item.children.length > 0
    }
    
    return true
  })
}

// 获取扁平化的菜单项（用于面包屑等）
export function getFlattenedMenuItems(userRole, userPermissions = []) {
  const filteredItems = getFilteredMenuItems(userRole, userPermissions)
  const flattened = []
  
  function flatten(items) {
    items.forEach(item => {
      flattened.push(item)
      if (item.children) {
        flatten(item.children)
      }
    })
  }
  
  flatten(filteredItems)
  return flattened
}

// 根据路径查找菜单项
export function findMenuItemByPath(path, userRole, userPermissions = []) {
  const flattenedItems = getFlattenedMenuItems(userRole, userPermissions)
  return flattenedItems.find(item => item.path === path)
}

// 获取面包屑导航
export function getBreadcrumbItems(currentPath, userRole, userPermissions = []) {
  const flattenedItems = getFlattenedMenuItems(userRole, userPermissions)
  const breadcrumbs = []
  
  // 查找当前路径对应的菜单项
  const currentItem = flattenedItems.find(item => item.path === currentPath)
  if (currentItem) {
    breadcrumbs.push({
      title: currentItem.title,
      path: currentItem.path
    })
    
    // 如果有父菜单，添加到面包屑
    const parentItem = MENU_ITEMS.find(parent => 
      parent.children && parent.children.some(child => child.key === currentItem.key)
    )
    if (parentItem) {
      breadcrumbs.unshift({
        title: parentItem.title,
        path: parentItem.path
      })
    }
  }
  
  return breadcrumbs
}
